package com.xhs.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;

import javax.servlet.http.HttpServletRequest;

/**
 * 日志配置
 */
@Configuration
@Slf4j
public class LoggingConfig {
    
    @Bean
    public CommonsRequestLoggingFilter requestLoggingFilter() {
        CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter() {
            @Override
            protected void beforeRequest(HttpServletRequest request, String message) {
                long startTime = System.currentTimeMillis();
                request.setAttribute("startTime", startTime);
                log.info("Request Started: {} {} - {}", request.getMethod(), request.getRequestURI(), message);
            }
            
            @Override
            protected void afterRequest(HttpServletRequest request, String message) {
                Long startTime = (Long) request.getAttribute("startTime");
                if (startTime != null) {
                    long duration = System.currentTimeMillis() - startTime;
                    log.info("Request Completed: {} {} - Duration: {}ms - {}", 
                            request.getMethod(), request.getRequestURI(), duration, message);
                }
            }
        };
        
        loggingFilter.setIncludeQueryString(true);
        loggingFilter.setIncludePayload(true);
        loggingFilter.setMaxPayloadLength(10000);
        loggingFilter.setIncludeHeaders(false);
        loggingFilter.setAfterMessagePrefix("REQUEST DATA : ");
        
        return loggingFilter;
    }
} 